package custom;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

import com.hichip.campro.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import bean.TimeArrayBean;
import utils.HiTools;

public class TimeDomainView extends View {
    private int screenH;
    private int screenW;
    private Paint mPaint;
    private int mDp_1;
    private int mAverage;
    private int mDp_15;
    private int mDp_10;
    private float mAverage_time;
    private Context mContext;

    public TimeDomainView(Context context) {
        super(context);
        this.mContext = context;
    }

    public TimeDomainView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        mPaint = new Paint();
        mDp_1 = HiTools.dip2px(context, 1);
        mDp_10 = HiTools.dip2px(context, 10);
        mDp_15 = HiTools.dip2px(context, 15);

        Activity act = (Activity) context;
        DisplayMetrics dm = new DisplayMetrics();
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenW = dm.widthPixels;
        screenH = dm.heightPixels;
        Log.i("tedu", "-screenW-:" + screenW + "-screenH-:" + screenH);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mAverage = getWidth() / 5;
        mPaint.setColor(Color.parseColor("#888888"));
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(getResources().getDimension(R.dimen.sp_13));

        int x_0 = 0;
        int x_6 = mAverage + mDp_1;
        int x_12 = mAverage * 2 + mDp_15 + mDp_1 * 4;
        int x_18 = mAverage * 3 + mDp_15 * 2 + mDp_1 * 8;
        int x_24 = mAverage * 4 + mDp_15 * 2 + mDp_1 * 10;

        if (HiTools.checkDeviceHasNavigationBar(mContext)) {//适配华为有虚拟按键的
            x_12 -= mDp_1 * 3;
            x_18 -= mDp_1 * 6;
            x_24 -= mDp_1 * 8;
        }
        float rateP = (float) screenH / screenW;
        float rate = 92f / 45;
        if (rateP == rate) {//适配曲面屏
            x_6 -= mDp_1 * 2;
            x_12 -= mDp_1 * 3;
            x_18 -= mDp_1 * 6;
            x_24 -= mDp_1 * 8;
        }

        canvas.drawText("00:00", x_0, mDp_10 + mDp_1, mPaint);
        canvas.drawText("06:00", x_6, mDp_10 + mDp_1, mPaint);
        canvas.drawText("12:00", x_12, mDp_10 + mDp_1, mPaint);
        canvas.drawText("18:00", x_18, mDp_10 + mDp_1, mPaint);
        canvas.drawText("23:59", x_24, mDp_10 + mDp_1, mPaint);
        RectF rectF = new RectF(0, mDp_15, getWidth(), getHeight());
        mPaint.setColor(Color.parseColor("#eeeeee"));
        canvas.drawRect(rectF, mPaint);
        mPaint.setColor(Color.parseColor("#45A7E6"));
        mPaint.setStyle(Paint.Style.FILL);
        if (array_rect.size() > 0) {
            for (RectF re : array_rect) {
                canvas.drawRect(re, mPaint);
            }
        }
        mAverage = (int) (rectF.width() / 4);
        mPaint.setColor(Color.parseColor("#777777"));
        mPaint.setStrokeWidth(mDp_1);
        canvas.drawLine(0 + mDp_1, mDp_15, 0 + mDp_1, getHeight(), mPaint);
        canvas.drawLine(mAverage, mDp_15, mAverage, getHeight(), mPaint);
        canvas.drawLine(mAverage * 2, mDp_15, mAverage * 2, getHeight(), mPaint);
        canvas.drawLine(mAverage * 3, mDp_15, mAverage * 3, getHeight(), mPaint);
        canvas.drawLine(mAverage * 4, mDp_15, mAverage * 4, getHeight(), mPaint);

    }

    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    private ArrayList<RectF> array_rect = new ArrayList<>();

    public void drawTimeDomain(TimeArrayBean bean) {
        if (bean.mIsAllDay) {
            RectF rectF = new RectF(0, mDp_15, getWidth(), getHeight());
            array_rect.add(rectF);
        } else {
            mAverage_time = (float) getWidth() / (24 * 60);
            array_rect.clear();
            for (String str : bean.getArrs()) {
                String[] strs = str.split("-");
                String str1 = strs[0];
                String str2 = strs[1];
                try {
                    Date date1 = sdf.parse(str1);
                    Date date2 = sdf.parse(str2);
                    int time1 = date1.getHours() * 60 + date1.getMinutes();
                    int time2 = date2.getHours() * 60 + date2.getMinutes();
                    int left = (int) (time1 * mAverage_time);
                    int right = (int) (time2 * mAverage_time);
                    RectF rectF = new RectF(left, mDp_15, right, getHeight());
                    array_rect.add(rectF);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        invalidate();
    }


}




